using System.Data;
using System.Collections;
using Microsoft.VisualBasic;
using System.Diagnostics;
using System;
using Microsoft.ReportingServices.ReportRendering;

namespace CustomExcelRenderCS
{
	internal class TextBoxRenderer
    {
        private const string cFinZeroNumber = "c0";
        private const string cFinFormat ="mso-number-format:'\\0022$\\0022\\#\\,\\#\\#0_\\)\\;\\[Red\\]\\\\\\(\\0022$\\0022\\#\\,\\#\\#0\\\\\\)'";        // financial number format
        private const string cFinFormat2 = "mso-number-format:'\\0022$\\0022\\#\\,\\#\\#0\\.00_\\)\\;\\[Red\\]\\\\\\(\\0022$\\0022\\#\\,\\#\\#0\\.00\\\\\\)'";        // financial number format
        private const string cTxtFormat = "mso-number-format:'\\@'";
		internal TextBoxRenderer()
		{
			
		}

		internal string Render(Microsoft.ReportingServices.ReportRendering.TextBox textBox)
		{
			System.Text.StringBuilder pHtml = new System.Text.StringBuilder();
            Microsoft.ReportingServices.ReportRendering.Style reportStyle = textBox.Style;
            StyleHandler sh = new StyleHandler(textBox.Style);

            string addFormat = "";
            addFormat = (textBox.Value !=null && textBox.Value.Contains("$")) ? (textBox.Style["Format"] != null ? (textBox.Style["Format"].ToString().ToLower().Equals(cFinZeroNumber) ? cFinFormat : cFinFormat2) : "") : "";
            addFormat = (textBox.Value != null && textBox.Value.StartsWith("0")) || (textBox.Style["Format"] != null && textBox.Style["Format"].Equals("t")) ? cTxtFormat : addFormat;

            pHtml.AppendFormat("<DIV STYLE=\"backgroundcolor:{0}\">", sh.GetStyleColorRGBCode("BackgroundColor"));
			pHtml.AppendFormat("<FONT FONT=\"{0}\" SIZE=\"{1}\" COLOR=\"{2}\" STYLE=\"font-size:{1};\">", sh.GetStyleString("FontFamily"), sh.GetStyleFontSize().ToPoints().ToString() + "pt", sh.GetStyleColorRGBCode("Color"));
			if (sh.GetStyleFontStyle() == System.Drawing.FontStyle.Italic)
			{

				pHtml.Append("<I>");
				
			}
			if (sh.GetStyleFontWeight() == System.Drawing.FontStyle.Bold)
			{
				pHtml.Append("<B>");
			}
			if (sh.GetStyleTextAlignment() == System.Drawing.ContentAlignment.MiddleCenter)
			{
                pHtml.AppendFormat("<DIV style=\"text-align:left;vertical-align:{0};{1}\">", reportStyle["VerticalAlign"],addFormat);               //"<CENTER>");
			}
			else if (sh.GetStyleTextAlignment() == System.Drawing.ContentAlignment.MiddleLeft)
			{
                pHtml.AppendFormat("<DIV style=\"text-align:left; vertical-align:{0};{1}\">", reportStyle["VerticalAlign"],addFormat);               //"<DIV ALIGN=\"LEFT\">");
			}
			else if (sh.GetStyleTextAlignment() == System.Drawing.ContentAlignment.MiddleRight)
			{
                pHtml.AppendFormat("<DIV style=\"text-align: right; vertical-align:{0};{1}\">", reportStyle["VerticalAlign"],addFormat);               //"<DIV ALIGN=\"LEFT\">");                                 // ALIGN=\"RIGHT\">");
			}
			if (textBox.Value == null || textBox.Value == "")
			{
				pHtml.Append("&nbsp;");
			}
			else
			{
                string textBoxValue = textBox.Value;
                if (textBox.Formula != null)
                {
                    textBoxValue = doExcelFormula(textBox);
                }
                 
				pHtml.Append(textBoxValue);
                
			}
			
			if (sh.GetStyleTextAlignment() == System.Drawing.ContentAlignment.MiddleCenter)
			{
				pHtml.Append("</CENTER>");
				
			}
			else
			{
				pHtml.Append("</DIV>");
			}
			if (sh.GetStyleFontWeight() == System.Drawing.FontStyle.Bold)
			{
				pHtml.Append("</B>");
			}
			if (sh.GetStyleFontStyle() == System.Drawing.FontStyle.Italic)
			{
				pHtml.Append("</I>");
			}
			
			pHtml.Append("</FONT></DIV>");
			return pHtml.ToString();
		}

        /// <summary>
        /// call FormulaHandler object to get back translated Excel formula string and update value field
        /// </summary>
        private string doExcelFormula(TextBox textBox)
        {

            TextBoxFormulaHandler formulaHandler = new TextBoxFormulaHandler();
            formulaHandler.targetReportItem = textBox;
            return formulaHandler.getFormula();
            
        }
	}
	
}
